package com.apes.scm.masterdata.stock.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.BranchEntry;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;

/**
 * 功能：库区属性
 *
 * @author xul
 * @create 2018-01-26 15:55
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = StockUsage.CONST_TABLE_NAME)
public class StockUsage extends Model {
    public static final String CONST_TABLE_NAME = "mas_usage";

    @Id
    @Column(name = "id", length = 8)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "库区类型")
    private String id;

    @Column(name = "name", length = 16)
    private String name; //名称

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "storage_condition", referencedColumnName = "id")
    private Enumeration storageCondition;  //存储条件

    @Column(name = "product_rule", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("带安虚拟仓标记")
    private boolean productRule;//带安虚拟仓标记

    @OneToMany(mappedBy = "usage", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<UsageBusiness> usageBusinesses = new HashSet(); //库区业务单据配置

    public Set<UsageBusiness> getUsageBusinesses() {
        if(usageBusinesses != null) {
            for (UsageBusiness item : usageBusinesses) {
                item.setUsage(this);
            }
        }
        return usageBusinesses;
    }

}
